//
//  DeviceDetailViewController2.m
//  CityFire
//
//  Created by  androidlongs on 2017/11/2.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "DeviceDetailViewController2.h"
#import "DeviceDetailInfoTableViewCell.h"
#import "AlertDeviceDetailViewViewController.h"
#import "UITextView+Placeholder.h"

@interface DeviceDetailViewController2 ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)  UITableView *tableView;
@end

@implementation DeviceDetailViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设备详情";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

-(void)viewDidAppear:(BOOL)animated{
    MonitorModel *model = self.model;
    NSString *str = [NSString stringWithFormat:@"%ld",model.timeHistory];
    NSString *breaktime = [NSString timeWithTimeIntervalString:str];
    if (self.type==1) {
        self.itemsArray= @[@[[NSString stringWithFormat:@"设备名称：%@", model.deviceName], [NSString stringWithFormat:@"设备编号：%ld", model.deviceCode], [NSString stringWithFormat:@"时间：%@", breaktime], [NSString stringWithFormat:@"来源：%@", model.record1], [NSString stringWithFormat:@"类型：%@", model.record2], [NSString stringWithFormat:@"内容：%@", model.record3]], @[[NSString stringWithFormat:@"设备信息：%@", model.deviceName], [NSString stringWithFormat:@"设备类型：%@", model.deviceType], [NSString stringWithFormat:@"控制器厂商：%@", model.controllerManufacturer], [NSString stringWithFormat:@"控制器型号：%@", model.controllerSpec], [NSString stringWithFormat:@"产品编号：%@", model.itemCode], [NSString stringWithFormat:@"探测器数量：%ld", model.detectorAmount1], [NSString stringWithFormat:@"采购时间：%@", model.purchaseTime], [NSString stringWithFormat:@"设备所在地：%@", model.deviceAddress], [NSString stringWithFormat:@"维保年限：%@", model.wbnx], [NSString stringWithFormat:@"维护联系人：%@", model.linkMan], [NSString stringWithFormat:@"联系人电话：%@", model.phone]], @[[NSString stringWithFormat:@"经度：%@", model.longitude], [NSString stringWithFormat:@"纬度：%@", model.latitude]], @[[NSString stringWithFormat:@"联系电话：%@", model.phone]]];
    }else{
        self.itemsArray = @[@[[NSString stringWithFormat:@"设备类型：%@", model.deviceType1], [NSString stringWithFormat:@"设备编号：%ld", model.deviceCode1], [NSString stringWithFormat:@"设备名称：%@", model.deviceName1], [NSString stringWithFormat:@"设备所在单位：%@", model.deviceOrg], [NSString stringWithFormat:@"设备所在地址：%@", model.deviceAddress1]], @[ [NSString stringWithFormat:@"经度：%@", model.latitude],[NSString stringWithFormat:@"纬度：%@", model.longitude]],@[[NSString stringWithFormat:@"探测器数量：%ld", model.detectorAmount1], [NSString stringWithFormat:@"控制器型号：%@", model.controllerSpec1], [NSString stringWithFormat:@"控制器厂家：%@", model.controllerManufacturer1], [NSString stringWithFormat:@"产品编号：%@", model.itemCode1], [NSString stringWithFormat:@"采购时间：%@", model.purchaseTime1]],@[[NSString stringWithFormat:@"售后电话：%@", model.hotline], [NSString stringWithFormat:@"维护联系人：%@", model.linkMan1], [NSString stringWithFormat:@"维保年限：%@", model.wbnx], [NSString stringWithFormat:@"维护联系人电话：%@", model.phone]],@[[NSString stringWithFormat:@"备注：%@", model.remark]]];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, mainW, mainH-64)];
        [_tableView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
        [self.view addSubview:_tableView];
        [self.view setBackgroundColor:[UIColor whiteColor]];
    }
    return _tableView;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.itemsArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.itemsArray[section] count];
}
//头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    return headerView;
}

//脚视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 4;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
    return footerView;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *ID = @"DeviceDetailInfoTableViewCell";
    // 根据标识去缓存池找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 不写这句直接崩掉，找不到循环引用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    }
    NSString *msg =self.itemsArray[indexPath.section][indexPath.row];
    if (indexPath.section==0&&indexPath.row==1) {
        cell.accessoryType = UITableViewCellAccessoryNone; //显示最右边的箭头
    } else {
        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text =msg;
    
    
    
    return cell;
}
//Cell点击事件的监听
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //       取消cell被选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *msg =self.itemsArray[indexPath.section][indexPath.row];
    
    if (indexPath.section==0&&indexPath.row==1) {
    }else{
        AlertDeviceDetailViewViewController *alertControll = [[AlertDeviceDetailViewViewController alloc]init];
        alertControll.alertTitleKey = msg;
        alertControll.sesion=indexPath.section;
        alertControll.raw = indexPath.row;
        alertControll.model = self.model;
        
        alertControll.callBackBlock = ^(MonitorModel *monitorModel) {
            self.model = monitorModel;
        };
        [self.navigationController pushViewController:alertControll animated:YES];
    }
}
@end
